#ifndef _ERROR_H_
#define _ERROR_H_

#include <stdarg.h>

//#ifdef __cplusplus
//extern "C" {
//#endif

void fatal(const char *fmt, ...);
void __trace_printf(char *channel, const char *_fmt, ...);

//#ifdef __cplusplus
//}
//#endif


static char *__trace_channel = NULL;


void init_console();


#ifndef VERBOSE_LEVEL
# define VERBOSE_LEVEL 0
#endif


#define xstr(s) str(s)
#define str(s) #s

//#define trace2(x ...) do { __trace_printf(__trace_channel,  #x "    (" __FILE__ ":" xstr(__LINE__) ")" ); }while(0)

//#define trace3(fmt, x) ({ typeof (x) _x = (x); __trace_printf(__trace_channel, "returning "fmt, _x); _x; })


#define trace(x ...) do { __trace_printf("TRACE", ""x); }while(0)

#define trace1(x ...)
#define trace2(x ...)

#if VERBOSE_LEVEL >= 1
# undef  trace1
# undef  trace2
# define trace1(ch, x ...) static char __trace_channel[]=ch; trace2(x)
# define trace2(x ...) do { __trace_printf(__trace_channel, ""x); }while(0)
#endif


#define trace3(x ...)
#define trace4(x ...)
#define trace5(x ...)
#define trace6(x ...)
#define trace7(x ...)
#define trace8(x ...)
#define trace9(x ...)


#if VERBOSE_LEVEL >= 3
# undef  trace3
# define trace3 trace2
#endif

#if VERBOSE_LEVEL >= 4
# undef  trace4
# define trace4 trace2
#endif

#if VERBOSE_LEVEL >= 5
# undef  trace5
# define trace5 trace2
#endif

#if VERBOSE_LEVEL >= 6
# undef  trace6
# define trace6 trace2
#endif

#if VERBOSE_LEVEL >= 7
# undef  trace7
# define trace7 trace2
#endif

#if VERBOSE_LEVEL >= 8
# undef  trace8
# define trace8 trace2
#endif

#if VERBOSE_LEVEL >= 9
# undef  trace9
# define trace9 trace2
#endif



#endif /* _ERROR_H_ */

